<template>
  <section>
    <!--提示-->
    <!--<el-alert title="" type="warning">开始时间：2016-12-31 结束时间：2017-01-09 操作用户：admin 操作结果：失败</el-alert>-->
    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item>
          <Search :allOpts="formInline.allOpts" v-model="formInline.cOpts" @blur="search"></Search>
        </el-form-item>
      </el-form>
    </el-col>
    <!--表格-->
    <template>
      <el-table :data="tableData" border style="width: 100%;">
        <el-table-column style="text-align: center;" prop="operator_user_name" label="操作用户"></el-table-column>
        <el-table-column prop="ip" label="用户IP"></el-table-column>
        <el-table-column label="操作行为">
          <template scope="scope">
            {{scope.row.opr_function | handleType}}
          </template>
        </el-table-column>
        <el-table-column prop="opr_action" label="操作功能"></el-table-column>
        <el-table-column label="操作结果">
          <template scope="scope">
            {{scope.row.opr_result | runResult}}
          </template>
        </el-table-column>
        <el-table-column prop="create_time" label="操作时间"></el-table-column>
        <el-table-column prop="opr_details" label="操作详情"></el-table-column>
      </el-table>
    </template>

    <!--分页-->
    <el-col :span="24" class="toolbar" style="padding-bottom:10px;">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageParm.startIndex"
        :page-sizes="[5, 10, 20, 50]"
        :page-size="pageParm.pageSize"
        layout="total ,sizes, prev, pager, next, jumper"
        :total="pageParm.count"
        style="float:right">
      </el-pagination>
    </el-col>
  </section>
</template>

<script>
  import util from '../../common/util'
  import dao from '../../common/dao'
  import Search from '../../components/Search.vue'

  export default {
    components: {Search},

    data() {
      return {
        tableData: [],
        startDate: "", //开始时间
        endDate: "", //结束时间
        user_id: "", //操作用户id
        opr_result: "", //操作结果
        pageParm: {
          startIndex: 1,
          pageCount: 20,
          count: 10,
          pageSize: 20
        },
        formInline: {
          allOpts: [{
            key: 'operator_user_name',
            label: '操作用户',
            type: '',
            val: ''
          }, {
            key: 'opr_result',
            label: '操作结果',
            type: 'sel',
            selOption: [{"label": "成功", "value": "0"}, {"label": "失败", "value": "1"}],
            val: ''
          }, {
            key: 'start_time',
            label: '开始时间',
            type: 'date',
            val: Date.parse(new Date()) - 24 * 60 * 60 * 1000
          }, {
            key: 'end_time',
            label: '结束时间',
            type: 'date',
            val: new Date()
          }],
          cOpts: [{
            key: 'start_time',
            label: '开始时间',
            type: 'date',
            val: Date.parse(new Date()) - 24 * 60 * 60 * 1000
          }, {
            key: 'end_time',
            label: '结束时间',
            type: 'date',
            val: new Date()
          }]
        }
      }
    },
    methods: {
      getAuditLogHttp () { //请求操作日志审计查询
        let d = new Date().setHours(23,59,59,0);
        let timestamp = (d -  24 * 60 * 60 * 1000) / 1000;
        let arg = {"start_index": this.pageParm.startIndex, "page_count": this.pageParm.pageSize};
        this.formInline.cOpts.map(item => {
          if (item.key == "start_time") {
            (typeof (item.val) == "number") ? arg.start_time = timestamp : arg.start_time = item.val.setHours(0,0,0,0) / 1000;
          } else if (item.key == "end_time") {
            arg.end_time = item.val.setHours(23, 59, 59, 0) / 1000;
          } else if (item.key == "opr_result") {
            arg.opr_result = item.val;
          } else if (item.key == "operator_user_name") {
            arg.operator_user_name = item.val;
          }
        });
        dao.getAuditLog(arg).then((response) => {
          // success callback
          if (response.body.retCode != 200) { return false }
          let pageRes = response.body;
          this.tableData = pageRes.results;

          this.pageParm.count = parseInt(pageRes.count);
          this.pageParm.pageSize = parseInt(pageRes.page_count);
        })

      },
      search (val) {
        //调用查询服务
        this.getAuditLogHttp();
      },
      handleSizeChange: function (val) {
        this.pageParm.pageSize = val;
        this.getAuditLogHttp();
      },
      handleCurrentChange: function (val) {
        this.pageParm.startIndex = val;
        this.getAuditLogHttp();
      }
    },
    mounted () {
      this.getAuditLogHttp();
    }
  }
</script>

<style scoped>
  .el-alert {
    margin-bottom: 20px;
  }
</style>